/**
 *
 */
package com.bsoft.gol.hcb.dao.evaluation;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import org.atag.dao.util.CustomAliasToEntityMapResultTransformer;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import pcn.sign.BaseEvaluationItem;

import java.util.ArrayList;
import java.util.List;

/**
 * @since 2017-05-02 12:55:02
 */
@SsdevDao(serviceId = "baseEvaluationItemDAOService")
public abstract class BaseEvaluationItemDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<BaseEvaluationItem> {

	public BaseEvaluationItemDAO() {
		this.setKeyField("id");
	}

	@DAOMethod(sql = "from BaseEvaluationItem where id=:id")
	public abstract BaseEvaluationItem getBaseEvaluationItemById(@DAOParam("id") int id);

	/**
	 * 根据id获取评价项目内容
	 *
	 * @param id 主键
	 * @return 评价项目内容
	 */
	@RpcService
	@DAOMethod(sql = "select content from BaseEvaluationItem where id=:id ")
	public abstract String getItemContentById(@DAOParam("id") Integer id);

	@DAOMethod(sql = "from BaseEvaluationItem where id=:id and defineId=:defineId")
	public abstract BaseEvaluationItem getBaseEvaluationItemByIdAndDefineId(@DAOParam("id") int id, @DAOParam(
	        "defineId") int defineId);


	@DAOMethod(sql = "from BaseEvaluationItem where defineId=:defineId and status='1' and isEnable=1")
	public abstract List<BaseEvaluationItem> findBaseEvaluationItemByDefineIdAndStatus(@DAOParam("defineId") int defineId);

	public List<BaseEvaluationItem> getBaseEvaluationItemByDefineId(final int defineId) {
		HibernateStatelessResultAction<List<BaseEvaluationItem>> action =
                new AbstractHibernateStatelessResultAction<List<BaseEvaluationItem>>() {
			@Override
			public void execute(StatelessSession ss) throws Exception {
				StringBuffer hsql = new StringBuffer();
				hsql.append("select * from base_evaluation_item t where t.define_id=:defineId and is_enable=1");
				Query query = ss.createSQLQuery(hsql.toString());
				query.setParameter("defineId", defineId);
				query.setResultTransformer(CustomAliasToEntityMapResultTransformer.INSTANCE);
				List listi = query.list();
				if (listi.size() == 0) {
					setResult(null);
				} else {

					List<BaseEvaluationItem> list = new ArrayList<>();
					for (Object o : listi) {
						list.add(ConversionUtils.convert(o, BaseEvaluationItem.class));
					}

					setResult(list);
				}
			}
		};
		HibernateSessionTemplate.instance().execute(action);
		return action.getResult();
	}


	@DAOMethod(sql = " update  BaseEvaluationItem set content=:content,status=:status  where id =:id")
	public abstract void updateById(@DAOParam("id") int id, @DAOParam("content") String content,
                                    @DAOParam("status") String status);

	/**
	 * 删除评价项目
	 *
	 * @param id
	 */
	@DAOMethod(sql = " update  BaseEvaluationItem set isEnable=0  where id =:id")
	public abstract void updateIsEnableById(@DAOParam("id") int id);

}